package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.cache.CacheRemoveMore;
import pers.cj.pdos.entity.Attr;
import pers.cj.pdos.entity.Type;
import pers.cj.pdos.mapper.AttrMapper;
import pers.cj.pdos.service.IAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class AttrServiceImpl extends ServiceImpl<AttrMapper, Attr> implements IAttrService {
    @Autowired
    AttrMapper attrMapper;
    @Override
    public boolean checkAttrValue(String attrValue, String companyId) {
        return attrMapper.selectCount(new QueryWrapper<Attr>().eq("company_id",companyId).eq("attr_value",attrValue)) > 0;
    }

    @Override
    @CacheRemoveMore(cacheRemoves = {
            //因为批量删除的功能是只能删除属性名下对应的属性的批量删除，可以用此方法来去除缓存
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "listByAttrName", param = "#attrList[0].companyId + ',' + #attrList[0].attrNameId"),
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "list", param = "#attrList[0].companyId"),
            @CacheRemove(clazz = AttrNameServiceImpl.class, methodName = "listVo", param = "#attrList[0].companyId")
    })
    public void deleteBatch(List<Attr> attrList) {
        attrMapper.deleteBatch(attrList);
    }

    @Override
    @CacheData(clazz = AttrServiceImpl.class, param = "#companyId + ',' + #attrNameId")
    public List<Attr> listByAttrName(String companyId, String attrNameId) {
        return attrMapper.selectList(new QueryWrapper<Attr>().eq("company_id",companyId).eq("attr_name_id",attrNameId));
    }

    @Override
    @CacheRemoveMore(cacheRemoves = {
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "listByAttrName", param = "#attr.companyId + ',' + #attr.attrNameId"),
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "list", param = "#attr.companyId"),
            @CacheRemove(clazz = AttrNameServiceImpl.class, methodName = "listVo", param = "#attr.companyId")
    })
    public void update(Attr attr) {
        attrMapper.updateById(attr);
    }

    @Override
    @CacheRemoveMore(cacheRemoves = {
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "listByAttrName", param = "#attr.companyId + ',' + #attr.attrNameId"),
            @CacheRemove(clazz = AttrServiceImpl.class, methodName = "list", param = "#attr.companyId"),
            @CacheRemove(clazz = AttrNameServiceImpl.class, methodName = "listVo", param = "#attr.companyId")
    })
    public void insert(Attr attr) {
        attrMapper.insert(attr);
    }

    @Override
    @CacheData(clazz = AttrServiceImpl.class, param = "#companyId")
    public List<Attr> list(String companyId) {
        return attrMapper.selectList(new QueryWrapper<Attr>().eq("company_id",companyId));
    }
}
